import 'dart:async';

import 'package:cdf_flutter/utils/screenutil_tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

import 'agree_privacy.dart';
import 'assets/assets_images.dart';
import 'hzy_normal_colors.dart';
import 'hzy_text_style.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({
    super.key,
  });

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  final UrlLauncherPlatform launcher = UrlLauncherPlatform.instance;
  Map<String, String?> params = {};
  Timer? _timer;

  AgreePrivacy agreePrivacy1 = AgreePrivacy(
    title: '隐私政策协议',
    message:
        '为更好地了解我们如何保护您的个人信息，现提供《隐私政策》（简要版）。 <br/><br/>1.收集您的手机号、性别、生日以及其他，是为实现账户注册、登录和验证以及识别会员身份的目的；收集您的证件信息、姓名、离岛信息、免税额度，是为了实现身份实名认证及离岛免税资格验证，若须邮寄，我们还可能收集您收件信息、订单信息。<br/><br/>2.为实现离岛身份验证，我们可能会申请您的摄像头使用权限；如禁止该权限，可能导致无法完成您的离岛身份认证。 <br/><br/>3.如您需要删除、销毁您的个人信息，可通过注销账号或撤回授权的方式。 <br/><br/>关于您个人信息更详细的内容，详见<a href="https://pic.cdfgsanya.com/assets/upload/protocol/protocol.html?id=10205">《隐私政策》</a> 全文，请您认真阅读并充分理解。我们会不断完善技术和安全管理，保护您的个人信息。',
    buttonAccept: "同意并接受",
    buttonRefuse: "暂不同意",
  );
  AgreePrivacy agreePrivacy2 = AgreePrivacy(
    title: '确认提示',
    message:
        '　　同意<a href="https://pic.cdfgsanya.com/assets/upload/protocol/protocol.html?id=10205">《隐私政策》</a> 才能继续使用中免海南，是否同意政策内容？',
    buttonAccept: "同意并继续",
    buttonRefuse: "退出APP",
  );
  late AgreePrivacy agreePrivacy;

  @override
  void initState() {
    super.initState();
    agreePrivacy = agreePrivacy1;
    WidgetsBinding.instance.addPostFrameCallback((_) => jumpPage());
  }

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  jumpPage() {
    showAgreePrivacyDialog();
  }

  @override
  Map<String, dynamic>? configNetWorkParmas(
      {String? mark, Map<String, dynamic>? params}) {
    return params;
  }

  ///网络请求
  @override
  getNetWorkData({int? type, Map<String, dynamic>? info}) async {}

  showAgreePrivacyDialog() {
    Widget body = Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        // Gaps.vGap10,
        SizedBox(
          height: 10,
        ),
        Text(
          agreePrivacy.title ?? '',
          style: HzyTextStyle.fontBold(size: 17),
        ),
        // Gaps.vGap20,
        SizedBox(
          height: 20,
        ),
        HtmlWidget(
          agreePrivacy.message ?? '',
          // onTapUrl: (url) {
          //   // 打开链接或执行其他操作
          //   _launchInWebViewWithJavaScript(url);
          //   return true; // 返回 true 表示事件已处理
          // },
        ),
        // Gaps.vGap10,
        SizedBox(
          height: 10,
        ),

        Ink(
          width: 120,
          height: 40,
          child: InkWell(
            child: Center(
              child: Text(
                agreePrivacy.buttonAccept ?? '',
                style: HzyTextStyle.fontRegular(
                    color: HzyNormalColorS.theme, size: 17),
              ),
            ),
            onTap: () async {
              // await CdfSpTool.saveAgreePrivacy(type: true);
              jumpPage();
            },
          ),
        ),

        // Gaps.vGap10,
        SizedBox(
          height: 10,
        ),
        Ink(
          width: 120,
          height: 40,
          child: InkWell(
            child: Center(
              child: Text(
                agreePrivacy.buttonRefuse ?? '',
                style: HzyTextStyle.fontRegular(
                    color: HzyNormalColorS.col333333, size: 17),
              ),
            ),
            onTap: () async {
              if (agreePrivacy == agreePrivacy1) {
                agreePrivacy = agreePrivacy2;

                /// 关闭在打开
                // currentGoback();
                Navigator.pop(context);
                showAgreePrivacyDialog();
              } else {
                /// 退出app
                SystemNavigator.pop();
              }
            },
          ),
        ),
      ],
    );

    return showDialog(
      context: context,
      builder: (context) {
        return WillPopScope(
          child: Dialog(
            // insetPadding: EdgeInsets.zero,
            insetPadding: EdgeInsets.symmetric(horizontal: 20),
            child: Container(
              padding: EdgeInsets.all(10),
              child: body,
            ),
          ),
          onWillPop: () async => false, //重点此举
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    Widget body = Container(
      width: double.infinity,
      height: double.infinity,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImages.splashSplash,
          fit: BoxFit.cover, // Ensures full-screen display
        ),
      ),
    );

    body = Scaffold(
      backgroundColor: Colors.red,
      body: body,
    );
    return body;
  }

// Future<void> _launchInWebViewWithJavaScript(String url) async {
//   if (!await launcher.launch(
//     url,
//     useSafariVC: true,
//     useWebView: true,
//     enableJavaScript: true,
//     enableDomStorage: true,
//     universalLinksOnly: true,
//     headers: <String, String>{
//       'harmony_browser_page': 'pages/LaunchInAppPage'
//     },
//   )) {
//     throw Exception('Could not launch $url');
//   }
// }
}
